package com.muyu.product.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.muyu.common.core.text.Convert;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.product.domain.BrandInfo;
import com.muyu.product.domain.req.BrandInfoQueryReq;
import com.muyu.product.domain.req.BrandInfoSaveReq;
import com.muyu.product.domain.req.BrandInfoEditReq;
import com.muyu.product.service.BrandInfoService;
import com.muyu.common.core.web.page.TableDataInfo;

/**
 * 品牌信息Controller
 *
 * @author DongZeLiang
 * @date 2024-02-27
 */
@Api(tags = "品牌信息")
@RestController
@RequestMapping("/brand")
public class BrandInfoController extends BaseController {
    @Autowired
    private BrandInfoService brandInfoService;

    /**
     * 查询品牌信息列表
     */
    @ApiOperation("获取品牌信息列表")
    @RequiresPermissions("product:brand:list")
    @GetMapping("/list")
    public Result list(BrandInfoQueryReq brandInfoQueryReq) {
        boolean isPage = brandInfoQueryReq.getParams().get("isPage") == null || Convert.toBool(brandInfoQueryReq.getParams().get("isPage"), true);
        if (isPage){
            startPage();
        }
        List<BrandInfo> list = brandInfoService.list(BrandInfo.queryBuild(brandInfoQueryReq));
        return isPage ? getDataTable(list) : Result.success(list);
    }

    /**
     * 导出品牌信息列表
     */
    @ApiOperation("导出品牌信息列表")
    @RequiresPermissions("product:brand:export")
    @Log(title = "品牌信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BrandInfo brandInfo) {
        List<BrandInfo> list = brandInfoService.list(brandInfo);
        ExcelUtil<BrandInfo> util = new ExcelUtil<BrandInfo>(BrandInfo.class);
        util.exportExcel(response, list, "品牌信息数据");
    }

    /**
     * 获取品牌信息详细信息
     */
    @ApiOperation("获取品牌信息详细信息")
    @RequiresPermissions("product:brand:query")
    @GetMapping(value = "/{id}")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
    public Result<BrandInfo> getInfo(@PathVariable("id") Long id) {
        return Result.success(brandInfoService.getById(id));
    }

    /**
     * 新增品牌信息
     */
    @RequiresPermissions("product:brand:add")
    @Log(title = "品牌信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增品牌信息")
    public Result<String> add(@RequestBody BrandInfoSaveReq brandInfoSaveReq) {
        return toAjax(brandInfoService.save(BrandInfo.saveBuild(brandInfoSaveReq)));
    }

    /**
     * 修改品牌信息
     */
    @RequiresPermissions("product:brand:edit")
    @Log(title = "品牌信息", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}")
    @ApiOperation("修改品牌信息")
    public Result<String> edit(@PathVariable Long id, @RequestBody BrandInfoEditReq brandInfoEditReq) {
        return toAjax(brandInfoService.updateById(BrandInfo.editBuild(id,brandInfoEditReq)));
    }

    /**
     * 删除品牌信息
     */
    @RequiresPermissions("product:brand:remove")
    @Log(title = "品牌信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除品牌信息")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path", dataTypeClass = String.class, example = "1,2,3,4")
    public Result<String> remove(@PathVariable List<Long> ids) {
        return toAjax(brandInfoService.removeBatchByIds(ids));
    }
}
